package com.common.sp;

import android.text.TextUtils;

import com.curry.android.util.JSONHelper;
import com.curry.android.util.StringUtil;
import com.orangelife.mobile.util.LogE;
import com.orangelife.mobile.util.TimeUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * 本地保存的用户个人信息
 * Created by chenxiang on 2016/6/22.
 *
 * @author wgf
 * @since v3.8
 */
public class UserSp {

    private static final String USER_SP = "USER_SP";

    public static void setAccessToken(String accessToken) {
        SharedPreferUtil.getInstance().putString("accessToken", accessToken);
    }

    /** token */
    public static String getAccessToken() {
        return SharedPreferUtil.getInstance().getString("accessToken", "");
    }

    public static void setLoginID(String loginID) {
        SharedPreferUtil.getInstance().putString("loginID", loginID);
    }

    /** 用户ID */
    public static String getLoginID() {
        return SharedPreferUtil.getInstance().getString("loginID", "");
    }

    public static void setLoginName(String accessToken) {
        SharedPreferUtil.getInstance().putString("loginName", accessToken);
    }

    /** 手机号 */
    public static String getLoginName() {
        return SharedPreferUtil.getInstance().getString("loginName", "");
    }

    public static void setNickname(String nickname) {
        SharedPreferUtil.getInstance().putString("nickname", nickname);
    }

    /** 昵称 */
    public static String getNickname() {
        String nickName = SharedPreferUtil.getInstance().getString("nickname", "");
        if (TextUtils.isEmpty(nickName)) {
            nickName = getRegPhone();
            if (TextUtils.isEmpty(nickName)) {
                nickName = "Admin";
            }
        }
        return nickName;
    }

    public static void setProtraitImg(String protraitImg) {
        SharedPreferUtil.getInstance().putString("protraitImg", protraitImg);
    }

    /** 头像地址 */
    public static String getProtraitImg() {
        String img = SharedPreferUtil.getInstance().getString("protraitImg", "");
        return img;
    }

    public static void setRegPhone(String phone) {
        SharedPreferUtil.getInstance().putString("phone", phone);
    }

    /** 注册时的手机号 */
    public static String getRegPhone() {
        return SharedPreferUtil.getInstance().getString("phone", "");
    }

    public static void setPoint(String point) {
        SharedPreferUtil.getInstance().putString("point", point);
    }

    /** 积分 */
    public static String getPoint() {
        return SharedPreferUtil.getInstance().getString("point", "");
    }


    public static void setHasCert(String certification) {
        if ("1".equals(certification)) {/*0未认证,1已认证*/
            SharedPreferUtil.getInstance().putBoolean("hasCert", true);
        } else {
            SharedPreferUtil.getInstance().putBoolean("hasCert", false);
        }
    }

    /** 至少认证了一个小区， */
    public static boolean hasCert() {
        return SharedPreferUtil.getInstance().getBoolean("hasCert", false);
    }

    /** 在该小区认证时所填的名字，第一次为准，以后认证全部都是用该名字，不能修改 */
    public static void setCertName(String certName) {
        if (StringUtil.isBlank(certName)) {
            return;
        }
        SharedPreferUtil.getInstance().putString("certName", certName);
    }

    public static String getCertName() {
        return SharedPreferUtil.getInstance().getString("certName", "");
    }

    public static void setHasCertInCurrentComm(String certificationInCurrentComm) {
        if ("1".equals(certificationInCurrentComm)) {
            SharedPreferUtil.getInstance().putBoolean("hasCertInCurrentComm", true);
        } else {
            SharedPreferUtil.getInstance().putBoolean("hasCertInCurrentComm", false);
        }
    }

    /** 当前小区已认证 */
    public static boolean hasCertInCurrentComm() {
        return SharedPreferUtil.getInstance().getBoolean("hasCertInCurrentComm", false);
    }

    /** 设置签到日期 */
    public static void setSignInDate() {
        String jsonOld = SharedPreferUtil.getInstance().getString(SharedPreferUtil.APP_INFO, "signInDate", "");
        Map<String, Object> map = JSONHelper.jsonToMap(jsonOld);
        if (map == null) {
            map = new HashMap<>();
        }
        map.put(UserSp.getRegPhone(), TimeUtil.getDataTime("yyyyMMdd"));
        String jsonNew = JSONHelper.Map2Json(map);
        SharedPreferUtil.getInstance().putString(SharedPreferUtil.APP_INFO, "signInDate", jsonNew);
    }

    public static void setSignInDate(String date) {
        SharedPreferUtil.getInstance().putString(SharedPreferUtil.APP_INFO, "signInDate", date);
        LogE.e("signInDate = " + TimeUtil.getDataTime("yyyyMMdd"));
    }

    /** 获取用户签到日期 */
    public static boolean hasSign() {
        String jsonOld = SharedPreferUtil.getInstance().getString(SharedPreferUtil.APP_INFO, "signInDate", "");
        if (StringUtil.isBlank(jsonOld)) {
            return false;
        }
        Map<String, Object> map = JSONHelper.jsonToMap(jsonOld);
        if (map == null) {
            return false;
        }
        String oldDate = String.valueOf(map.get(UserSp.getRegPhone()));
        String currentDate = TimeUtil.getDataTime("yyyyMMdd");
        return currentDate.equals(oldDate);
    }

    public static void setRegisterTime(String registerTime) {
        SharedPreferUtil.getInstance().putString("registerTime", registerTime);
    }

    public static String getRegisterTime() {
        return SharedPreferUtil.getInstance().getString("registerTime", "");
    }

    public static String getIdentity(){
        return SharedPreferUtil.getInstance().getString("identity","");
    }

    public static void setIdentity(String identity){

        SharedPreferUtil.getInstance().putString("identity",identity);
    }

    public static void setCity(String city){
        SharedPreferUtil.getInstance().putString("city",city);
    }


    public static String getCity(){
        return SharedPreferUtil.getInstance().getString("city","");
    }
}
